package com.itheima.service.impl;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import com.itheima.service.ValidatecodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class ValidatecodeServiceImpl implements ValidatecodeService {

    private SMSUtils smsUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    public static final int EXPIRE_TIME = 300; // 验证码过期时间

    /**
     * 体检预约时发送验证码
     * @param telephone
     */
    @Override
    public void sendCode(String telephone) {
        // 用随机生成验证码工具类生成一个4位的数字验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        // 将体检预约时的验证码保存到redis中 键为telephone + RedisMessageConstant.SENDTYPE_ORDER 并设置过期时间为5分钟
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(telephone + RedisMessageConstant.SENDTYPE_ORDER, code, EXPIRE_TIME, TimeUnit.SECONDS);
        // 调用短信发送工具类发送短信 由于阿里云短信服务的签名未申请下来，故这步跳过
        //smsUtils.sendMessage(SMSUtils.VALIDATE_CODE, telephone, code);
    }

    /**
     * 手机号快速登录时发送验证码
     * @param telephone
     */
    @Override
    public void send4Login(String telephone) {
        // 用随机生成验证码工具类生成一个六位数的数字验证码
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        // 将手机号快速登录时的验证码保存到redis中 键为telephone + RedisMessageConstant.SENDTYPE_LOGIN 并设置过期时间为5分钟
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(telephone + RedisMessageConstant.SENDTYPE_LOGIN, code, EXPIRE_TIME, TimeUnit.SECONDS);
        // 调用短信发送工具类发送短信，由于阿里云短信服务的签名未申请下来，故这步跳过
        //smsUtils.sendMessage(SMSUtils.VALIDATE_CODE, telephone, code);
    }
}
